package com.atguigu.gmall.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.realtime.app.func.BeanToJsonStrFunction;
import com.atguigu.gmall.realtime.app.func.DimAsyncFunction;
import com.atguigu.gmall.realtime.beans.TradeSkuOrderBean;
import com.atguigu.gmall.realtime.utils.DateFormatUtil;
import com.atguigu.gmall.realtime.utils.DorisUtil;
import com.atguigu.gmall.realtime.utils.MyKafkaUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.StateTtlConfig;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.streaming.api.datastream.*;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

/**
 * @author Felix
 * @date 2023/11/20
 * 下单聚合统计
 * 需要启动的进程
 *      zk、kafka、maxwell、hdfs、hbase、redis、doris、
 *      DwdTradeOrderDetail、DwsTradeSkuOrderWindow
 * 开发思路总结
 *      环境准备
 *      从kafka的下单事实表主题中读取数据
 *      过滤空消息 并转换流中的数据类型    jsonStr->jsonObj
 *      去重
 *          为什么会产生重复数据？
 *              因为dwd的下单事实表主要由4张表组成：订单明细表(主)、订单、订单明细活动、订单明细优惠券
 *              这4张表在关联的时候，明细和订单是内连接，和明细活动、明细优惠券使用的是左外连接，如果左表
 *              先到，右表后到，会产生3条数据
 *              [左表     null]   +I
 *              [左表     null]   -D
 *              [左表     右边]    +I
 *              这样的数据，发送到kafka主题后，主题会接收到3条消息
 *              [左表     null]
 *              null
 *              [左表     右表]
 *              所以在从kafka主题中读取数据的时候，如果使用的是FlinkAPI提供的默认的反序列化SimpleStringSchema，
 *              是没有办法对空消息进行处理的，所以我们需要自定义反序列化类，处理空消息。并需要对剩余2条消息进行去重
 *      去重方式1：状态 + 定时器
 *          思路：第一条数据来到的时候，将其放到状态中，并注册5s后执行的定时器，
 *               第二条数据来到的时候，会用当前数据的聚合时间和状态中存在的数据聚合时间进行比较，将时间大的放到状态中
 *               当定时器被触发后，将状态中的数据发送到下游
 *          优点：传输数据量小
 *          缺点：时效性差
 *      去重方式2：状态 + 抵消
 *          思路：第一条数据来到的时候，将其放到状态中，并传递到下游
 *               第二条数据来到的时候，会将状态中影响到度量值的属性取反，再传递到下游
 *               同时将第二条数据发送到下游。
 *          优点：时效性好
 *          缺点：如果出现重复数据，会向下游发送3条
 *      再次对流中数据类型进行转换   jsonObj->实体类对象(维度 + 度量)
 *      指定Watermark以及提取事件时间字段
 *      按照sku进行分组
 *      开窗
 *      聚合计算
 *      关联维度
 *          基本的维度关联的实现
 *              HbaseUtil.getDimInfoFromHbase()--->维度jsonObj
 *          优化1：旁路缓存
 *              思路：
 *                  先从缓存中获取维度数据，如果在缓存中找到了要关联的维度，直接将其返回(缓存命中)；
 *                  如果在缓存中没有找到要关联的维度，发送请求到hbase中查询维度数据，并将查询的结果
 *                  放到缓存中缓存起来，方便下次查询使用
 *              缓存产品选型：
 *                  状态      性能好，维护性差
 *                  redis    性能还行，维护性好      √
 *              关于Redis的设置
 *                  key:    维度表名:主键值
 *                  type:   String
 *                  expire: 1day    防止冷数据常驻内存，给内存带来压力
 *              注意：如果维度数据发生了变化，需要将Redis中缓存的数据清除掉
 *                  DimSinkFunction-->执行完操作hbase代码之后--->DimUtil.deleteCached
 *          优化2：异步IO
 *              概念：
 *                  异步：同时执行
 *                  同步：排队
 *              为什么使用异步IO？
 *                  当对流中数据进行处理的时候，要想提升处理能力，可以将算子的并行度调大，但是更大的并行度意味着
 *                  需要更多的硬件资源，不可能无限制的调整。
 *                  如果使用的map算子处理流中数据的时候，在单个并行度上是同步的处理方式，也就是说，流中数据是处理完
 *                  一个再处理下一个，效率很低，尤其是和外部系统交互的时候。
 *                  所以需要使用异步的处理方式
 *              Flink提供了异步处理的API
 *                  AsyncDataStream.[un]orderedWait(
 *                      流，
 *                      异步操作 implements AsyncFunction,
 *                      超时时间，
 *                      时间单位
 *                  )
 *              Redis提供的异步操作的连接对象：StatefulRedisConnection
 *                  RedisUtil:
 *                      获取异步连接
 *                      关闭异步连接
 *                      以异步的方式从Redis中读取数据
 *                      以异步的方式向Redis中写数据
 *              Hbase提供的异步操作的连接对象:AsyncConnection
 *                  HbaseUtil：
 *                      获取异步连接
 *                      关闭异步连接
 *                      以异步的方式从Hbase中读取数据
 *             在对发送异步请求，进行维度关联聚合实现的时候，我们单位封装一个模板类
 *             模板方法设计模式：在父类中定义完成某一个功能的核心算法骨架，具体的某些步骤在父类中没有办法实现，
 *                  可以延迟到子类中去完成。好处，在不改变父类核心算法骨架的前提下，每一个子类都可以有自己不同
 *                  的实现
 *             class DimAsyncFunction extends RichAsyncFunction implements DimJoinFunction{
 *                  open:初始化连接
 *                  close:关闭连接
 *                  asyncInvoke：发送异步请求进行维度关联
 *                       CompletableFuture
 *                       .supplyAsync     创建异步编排对象   无参，有返回值
 *                       .thenApplyAsync  串行执行          有参，有返回值  上一个并行任务的返回值会作为当前任务的参数
 *                       .thenAcceptAsync 串行执行          有参，无返回值
 *             }
 *      将数据写到Doris
 */
public class DwsTradeSkuOrderWindow {
    public static void main(String[] args) throws Exception {
        //TODO 1.基本环境准备
        //1.1 指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //1.2 设置并行度
        env.setParallelism(4);
        //TODO 2.检查点相关的设置
        env.enableCheckpointing(5000L);
       /* //2.1 开启检查点
        env.enableCheckpointing(5000L, CheckpointingMode.EXACTLY_ONCE);
        //2.2 设置检查点超时时间
        env.getCheckpointConfig().setCheckpointTimeout(60000L);
        //2.3 设置job取消后检查点是否保留
        env.getCheckpointConfig().setExternalizedCheckpointCleanup(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
        //2.4 设置两个检查点之间最小时间间隔
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(2000L);
        //2.5 设置重启策略
        env.setRestartStrategy(RestartStrategies.failureRateRestart(3, Time.days(30),Time.seconds(3)));
        //2.6 设置状态后端
        env.setStateBackend(new HashMapStateBackend());
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop102:8020/gmall/ck");
        //2.7 设置操作hadoop的用户
        System.setProperty("HADOOP_USER_NAME","atguigu");*/

        //TODO 3.从kakfa主题中读取数据
        //3.1 声明消费的主题以及消费者组
        String topic = "dwd_trade_order_detail";
        String groupId = "dws_trade_sku_order_group";
        //3.2 创建消费者对象
        KafkaSource<String> kafkaSource = MyKafkaUtil.getKafkaSource(topic, groupId);
        //3.3 消费数据 封装为流
        DataStreamSource<String> kafkaStrDS
            = env.fromSource(kafkaSource, WatermarkStrategy.noWatermarks(), "kafka_source");
        //TODO 4.对流中数据进行类型转换并过滤掉空消息  jsonStr->jsonObj
        SingleOutputStreamOperator<JSONObject> jsonObjDS = kafkaStrDS.process(
            new ProcessFunction<String, JSONObject>() {
                @Override
                public void processElement(String jsonStr, Context ctx, Collector<JSONObject> out) throws Exception {
                    if (StringUtils.isNotEmpty(jsonStr)) {
                        JSONObject jsonObj = JSON.parseObject(jsonStr);
                        out.collect(jsonObj);
                    }
                }
            }
        );

        // jsonObjDS.print(">>>");
        //TODO 5.去重
        //5.1 按照唯一键(订单明细id)进行分组
        KeyedStream<JSONObject, String> orderDetailIdKeyedDS = jsonObjDS.keyBy(jsonObj -> jsonObj.getString("id"));
        //5.2 去重方式1：状态 + 定时器   不足：延迟大，影响时效性
        /*SingleOutputStreamOperator<JSONObject> distinctDS = orderDetailIdKeyedDS.process(
            new KeyedProcessFunction<String, JSONObject, JSONObject>() {
                private ValueState<JSONObject> lastJsonObjState;

                @Override
                public void open(Configuration parameters) throws Exception {
                    ValueStateDescriptor<JSONObject> valueStateDescriptor
                        = new ValueStateDescriptor<JSONObject>("lastJsonObjState", JSONObject.class);
                    lastJsonObjState = getRuntimeContext().getState(valueStateDescriptor);
                }

                @Override
                public void processElement(JSONObject jsonObj, Context ctx, Collector<JSONObject> out) throws Exception {
                    //从状态中获取当前订单明细id对应上次记录
                    JSONObject lastJsonObj = lastJsonObjState.value();
                    if (lastJsonObj == null) {
                        //说明还没有重复数据，将当前这条数据放到状态中
                        lastJsonObjState.update(jsonObj);
                        //注册一个5s之后执行的定时器
                        long currentProcessingTime = ctx.timerService().currentProcessingTime();
                        ctx.timerService().registerProcessingTimeTimer(currentProcessingTime + 5000L);
                    } else {
                        //说明有重复数据
                        //用当前这条数据的聚合时间和状态中数据的聚合时间进行比较
                        String ts1 = lastJsonObj.getString("聚合时间");
                        String ts2 = jsonObj.getString("聚合时间");
                        if (ts2.compareTo(ts1) >= 0) {
                            lastJsonObjState.update(jsonObj);
                        }
                    }

                }

                @Override
                public void onTimer(long timestamp, OnTimerContext ctx, Collector<JSONObject> out) throws Exception {
                    //定时器触发 会被执行的方法
                    //从状态中获取数据
                    JSONObject jsonObj = lastJsonObjState.value();
                    //发送到下游
                    out.collect(jsonObj);
                    //清状态
                    lastJsonObjState.clear();
                }
            }
        );*/
        //5.3 去重方式2：状态 + 抵消   优点：时效性好       缺点：传输的数据量变大
        SingleOutputStreamOperator<JSONObject> distinctDS = orderDetailIdKeyedDS.process(

            new KeyedProcessFunction<String, JSONObject, JSONObject>() {
                private ValueState<JSONObject> lastJsonObjState;

                @Override
                public void open(Configuration parameters) throws Exception {
                    ValueStateDescriptor<JSONObject> valueStateDescriptor
                        = new ValueStateDescriptor<JSONObject>("lastJsonObjState", JSONObject.class);
                    valueStateDescriptor.enableTimeToLive(StateTtlConfig.newBuilder(Time.seconds(10)).build());
                    lastJsonObjState = getRuntimeContext().getState(valueStateDescriptor);
                }

                @Override
                public void processElement(JSONObject jsonObj, Context ctx, Collector<JSONObject> out) throws Exception {
                    //判断是否重复
                    JSONObject lastJsonObj = lastJsonObjState.value();

                    if (lastJsonObj != null) {
                        //说明重复了，将状态中影响到度量值的字段进行取反，再发送到下游进行抵消
                        String splitOriginalAmount = lastJsonObj.getString("split_original_amount");
                        String splitCouponAmount = lastJsonObj.getString("split_coupon_amount");
                        String splitActivityAmount = lastJsonObj.getString("split_activity_amount");
                        String splitTotalAmount = lastJsonObj.getString("split_total_amount");

                        //取反，发送到下游去
                        lastJsonObj.put("split_original_amount", "-" + splitOriginalAmount);
                        lastJsonObj.put("split_coupon_amount", "-" + splitCouponAmount);
                        lastJsonObj.put("split_activity_amount", "-" + splitActivityAmount);
                        lastJsonObj.put("split_total_amount", "-" + splitTotalAmount);
                        out.collect(lastJsonObj);

                    }
                    lastJsonObjState.update(jsonObj);
                    out.collect(jsonObj);
                }
            }
        );
        // distinctDS.print(">>>>");
        //TODO 6.再次对流中数据类型进行转换 jsonObj->实体类对象(维度 + 度量)
        SingleOutputStreamOperator<TradeSkuOrderBean> orderBeanDS = distinctDS.map(
            new MapFunction<JSONObject, TradeSkuOrderBean>() {
                @Override
                public TradeSkuOrderBean map(JSONObject jsonObj) throws Exception {
                    // {"create_time":"2023-11-18 10:44:58","sku_num":"1","activity_rule_id":"5",
                    // "split_original_amount":"11999.0000","split_coupon_amount":"0.0","sku_id":"19",
                    // "date_id":"2023-11-18","user_id":"2910","province_id":"20","activity_id":"4",
                    // "sku_name":"TCL ","id":"14254996","order_id":"52894","split_activity_amount":"1199.9",
                    // "split_total_amount":"10799.1","ts":"1700448298"}
                    String skuId = jsonObj.getString("sku_id");
                    String splitOriginalAmount = jsonObj.getString("split_original_amount");
                    String splitCouponAmount = jsonObj.getString("split_coupon_amount");
                    String splitActivityAmount = jsonObj.getString("split_activity_amount");
                    String splitTotalAmount = jsonObj.getString("split_total_amount");
                    Long ts = jsonObj.getLong("ts") * 1000;
                    TradeSkuOrderBean orderBean = TradeSkuOrderBean.builder()
                        .skuId(skuId)
                        .originalAmount(new BigDecimal(splitOriginalAmount))
                        .couponAmount(new BigDecimal(splitCouponAmount))
                        .activityAmount(new BigDecimal(splitActivityAmount))
                        .orderAmount(new BigDecimal(splitTotalAmount))
                        .ts(ts)
                        .build();
                    return orderBean;
                }
            }
        );
        // orderBeanDS.print(">>>");
        //TODO 7.指定Watermark以及提取事件时间字段
        SingleOutputStreamOperator<TradeSkuOrderBean> withWatermarkDS = orderBeanDS.assignTimestampsAndWatermarks(
            WatermarkStrategy
                .<TradeSkuOrderBean>forMonotonousTimestamps()
                .withTimestampAssigner(
                    new SerializableTimestampAssigner<TradeSkuOrderBean>() {
                        @Override
                        public long extractTimestamp(TradeSkuOrderBean orderBean, long recordTimestamp) {
                            return orderBean.getTs();
                        }
                    }
                )
        );
        //TODO 8.按照统计维度skuId进行分组
        KeyedStream<TradeSkuOrderBean, String> skuIdKeyedDS
            = withWatermarkDS.keyBy(TradeSkuOrderBean::getSkuId);
        //TODO 9.开窗
        WindowedStream<TradeSkuOrderBean, String, TimeWindow> windowDS
            = skuIdKeyedDS.window(TumblingEventTimeWindows.of(org.apache.flink.streaming.api.windowing.time.Time.seconds(10)));
        //TODO 10.聚合计算
        SingleOutputStreamOperator<TradeSkuOrderBean> reduceDS = windowDS.reduce(
            new ReduceFunction<TradeSkuOrderBean>() {
                @Override
                public TradeSkuOrderBean reduce(TradeSkuOrderBean value1, TradeSkuOrderBean value2) throws Exception {
                    value1.setOriginalAmount(value1.getOriginalAmount().add(value2.getOriginalAmount()));
                    value1.setActivityAmount(value1.getActivityAmount().add(value2.getActivityAmount()));
                    value1.setCouponAmount(value1.getCouponAmount().add(value2.getCouponAmount()));
                    value1.setOrderAmount(value1.getOrderAmount().add(value2.getOrderAmount()));
                    return value1;
                }
            },
            new WindowFunction<TradeSkuOrderBean, TradeSkuOrderBean, String, TimeWindow>() {
                @Override
                public void apply(String s, TimeWindow window, Iterable<TradeSkuOrderBean> input, Collector<TradeSkuOrderBean> out) throws Exception {
                    String stt = DateFormatUtil.toYmdHms(window.getStart());
                    String edt = DateFormatUtil.toYmdHms(window.getEnd());
                    String curDate = DateFormatUtil.toDate(window.getStart());
                    for (TradeSkuOrderBean orderBean : input) {
                        orderBean.setStt(stt);
                        orderBean.setEdt(edt);
                        orderBean.setCurDate(curDate);
                        out.collect(orderBean);
                    }
                }
            }
        );
        // reduceDS.print(">>>>");

        //TODO 11.关联sku维度
        /*
        //基本维度关联实现
        SingleOutputStreamOperator<TradeSkuOrderBean> withSkuInfoDS = reduceDS.map(
            new RichMapFunction<TradeSkuOrderBean, TradeSkuOrderBean>() {

                private Connection conn;
                @Override
                public void open(Configuration parameters) throws Exception {
                    conn = HbaseUtil.getHbaseConnection();
                }

                @Override
                public void close() throws Exception {
                    HbaseUtil.closeHbaseConnection(conn);
                }

                @Override
                public TradeSkuOrderBean map(TradeSkuOrderBean orderBean) throws Exception {
                    //根据流中对象获取要关联的维度主键的id
                    String skuId = orderBean.getSkuId();

                    //根据id到hbase的维度表中获取对应的维度对象
                    //id,spu_id,price,sku_name,sku_desc,weight,tm_id,category3_id,sku_default_img,is_sale,create_time
                    JSONObject dimJsonObj
                        = HbaseUtil.getDimInfoFromHbase(conn, GmallConfig.HBASE_NAMESPACE, "dim_sku_info", skuId);
                    //将商品相关的维度属性补充到流中对象上
                    if(dimJsonObj != null){
                        orderBean.setSkuName(dimJsonObj.getString("sku_name"));
                        orderBean.setSpuId(dimJsonObj.getString("spu_id"));
                        orderBean.setTrademarkId(dimJsonObj.getString("tm_id"));
                        orderBean.setCategory3Id(dimJsonObj.getString("category3_id"));
                    }
                    return orderBean;
                }
            }
        );
        //优化：旁路缓存
        SingleOutputStreamOperator<TradeSkuOrderBean> withSkuInfoDS = reduceDS.map(
            new RichMapFunction<TradeSkuOrderBean, TradeSkuOrderBean>() {

                private Connection conn;
                private Jedis jedis;
                @Override
                public void open(Configuration parameters) throws Exception {
                    conn = HbaseUtil.getHbaseConnection();
                    jedis = RedisUtil.getRedisConnection();
                }

                @Override
                public void close() throws Exception {
                    HbaseUtil.closeHbaseConnection(conn);
                    RedisUtil.closeRedisConnection(jedis);
                }

                @Override
                public TradeSkuOrderBean map(TradeSkuOrderBean orderBean) throws Exception {
                    //根据流中对象获取要关联的维度主键的id
                    String skuId = orderBean.getSkuId();

                    //根据id到hbase的维度表中获取对应的维度对象
                    //id,spu_id,price,sku_name,sku_desc,weight,tm_id,category3_id,sku_default_img,is_sale,create_time
                    JSONObject dimJsonObj
                        = DimUtil.getDimInfo(jedis,conn, GmallConfig.HBASE_NAMESPACE, "dim_sku_info", skuId);
                    //将商品相关的维度属性补充到流中对象上
                    if(dimJsonObj != null){
                        orderBean.setSkuName(dimJsonObj.getString("sku_name"));
                        orderBean.setSpuId(dimJsonObj.getString("spu_id"));
                        orderBean.setTrademarkId(dimJsonObj.getString("tm_id"));
                        orderBean.setCategory3Id(dimJsonObj.getString("category3_id"));
                    }
                    return orderBean;
                }
            }
        );

        //抽取模板类（模板方法设计模式）
        SingleOutputStreamOperator<TradeSkuOrderBean> withSkuInfoDS = reduceDS.map(
            new DimMapFunction<TradeSkuOrderBean>("dim_sku_info") {
                @Override
                public void join(JSONObject dimJsonObj, TradeSkuOrderBean orderBean) {
                    orderBean.setSkuName(dimJsonObj.getString("sku_name"));
                    orderBean.setSpuId(dimJsonObj.getString("spu_id"));
                    orderBean.setTrademarkId(dimJsonObj.getString("tm_id"));
                    orderBean.setCategory3Id(dimJsonObj.getString("category3_id"));
                }

                @Override
                public String getKey(TradeSkuOrderBean orderBean) {
                    return orderBean.getSkuId();
                }
            }
        );

        //发送异步请求，处理流中数据   没有抽取异步模板
        SingleOutputStreamOperator<TradeSkuOrderBean> withSkuInfoDS = AsyncDataStream.unorderedWait(
            reduceDS,
            new RichAsyncFunction<TradeSkuOrderBean, TradeSkuOrderBean>() {
                private StatefulRedisConnection<String,String> asyncRedisConn;
                private AsyncConnection asyncHbaseConn;

                @Override
                public void open(Configuration parameters) throws Exception {
                    asyncRedisConn = RedisUtil.getAsyncRedisConnection();
                    asyncHbaseConn = HbaseUtil.getAsyncHbaseConnection();
                }

                @Override
                public void close() throws Exception {
                    RedisUtil.closeAsyncRedisConnection(asyncRedisConn);
                    HbaseUtil.closeAsyncHbaseConnection(asyncHbaseConn);
                }

                @Override
                public void asyncInvoke(TradeSkuOrderBean orderBean, ResultFuture<TradeSkuOrderBean> resultFuture) throws Exception {
                    //发送异步请求 处理过程
                    //根据流中的对象获取要关联的维度的主键
                    String skuId = orderBean.getSkuId();
                    //发送异步请求从redis中获取要关联的维度数据
                    JSONObject dimJsonObj = RedisUtil.asyncGetDimInfo(asyncRedisConn, "dim_sku_info:" + skuId);
                    //如果从redis中没有找到要关联的维度，发送异步请求从hbase中查询维度
                    if(dimJsonObj == null){
                        dimJsonObj = HbaseUtil.getDimInfoFromHbaseByAsync(asyncHbaseConn, GmallConfig.HBASE_NAMESPACE, "dim_sku_info", skuId);
                        //将查询到的维度数据  放到redis中缓存
                        if(dimJsonObj != null){
                            RedisUtil.asyncWriteDim(asyncRedisConn,"dim_sku_info:" + skuId,dimJsonObj);
                        }
                    }
                    //将维度相关的属性 补充到流中的对象上
                    if(dimJsonObj != null){
                        orderBean.setSkuName(dimJsonObj.getString("sku_name"));
                        orderBean.setSpuId(dimJsonObj.getString("spu_id"));
                        orderBean.setTrademarkId(dimJsonObj.getString("tm_id"));
                        orderBean.setCategory3Id(dimJsonObj.getString("category3_id"));
                    }
                    //将补充完维度的对象 传递到下游
                    resultFuture.complete(Collections.singleton(orderBean));
                }
            },
            60,
            TimeUnit.SECONDS
        );
        */
        //发送异步请求，关联sku维度 ---使用模板
        SingleOutputStreamOperator<TradeSkuOrderBean> withSkuInfoDS = AsyncDataStream.unorderedWait(
            reduceDS,
            new DimAsyncFunction<TradeSkuOrderBean>("dim_sku_info") {
                @Override
                public void join(JSONObject dimJsonObj, TradeSkuOrderBean orderBean) {
                    orderBean.setSkuName(dimJsonObj.getString("sku_name"));
                    orderBean.setSpuId(dimJsonObj.getString("spu_id"));
                    orderBean.setTrademarkId(dimJsonObj.getString("tm_id"));
                    orderBean.setCategory3Id(dimJsonObj.getString("category3_id"));
                }

                @Override
                public String getKey(TradeSkuOrderBean orderBean) {
                    return orderBean.getSkuId();
                }
            },
            60,
            TimeUnit.SECONDS
        );
        // withSkuInfoDS.print(">>>>");
        //TODO 12.关联spu维度
        SingleOutputStreamOperator<TradeSkuOrderBean> withSpuInfoDS = AsyncDataStream.unorderedWait(
            withSkuInfoDS,
            new DimAsyncFunction<TradeSkuOrderBean>("dim_spu_info") {
                @Override
                public void join(JSONObject dimInfoJsonObj, TradeSkuOrderBean orderBean) {
                    orderBean.setSpuName(dimInfoJsonObj.getString("spu_name"));
                }

                @Override
                public String getKey(TradeSkuOrderBean orderBean) {
                    return orderBean.getSpuId();
                }
            },
            60,
            TimeUnit.SECONDS
        );
        //TODO 13.关联tm维度
        SingleOutputStreamOperator<TradeSkuOrderBean> withTmDS = AsyncDataStream.unorderedWait(
            withSpuInfoDS,
            new DimAsyncFunction<TradeSkuOrderBean>("dim_base_trademark") {
                @Override
                public void join(JSONObject dimInfoJsonObj, TradeSkuOrderBean orderBean) {
                    orderBean.setTrademarkName(dimInfoJsonObj.getString("tm_name"));
                }

                @Override
                public String getKey(TradeSkuOrderBean orderBean) {
                    return orderBean.getTrademarkId();
                }
            },
            60,
            TimeUnit.SECONDS
        );
        //TODO 14.关联category3维度
        SingleOutputStreamOperator<TradeSkuOrderBean> c3Stream = AsyncDataStream.unorderedWait(
            withTmDS,
            new DimAsyncFunction<TradeSkuOrderBean>("dim_base_category3") {

                @Override
                public String getKey(TradeSkuOrderBean bean) {
                    return bean.getCategory3Id();
                }

                @Override
                public void join(JSONObject dim,TradeSkuOrderBean bean) {
                    bean.setCategory3Name(dim.getString("name"));
                    bean.setCategory2Id(dim.getString("category2_id"));
                }
            },
            60,
            TimeUnit.SECONDS
        );

        //TODO 15.关联category2维度
        SingleOutputStreamOperator<TradeSkuOrderBean> c2Stream = AsyncDataStream.unorderedWait(
            c3Stream,
            new DimAsyncFunction<TradeSkuOrderBean>("dim_base_category2") {

                @Override
                public String getKey(TradeSkuOrderBean bean) {
                    return bean.getCategory2Id();
                }

                @Override
                public void join(JSONObject dim,TradeSkuOrderBean bean
                                 ) {
                    bean.setCategory2Name(dim.getString("name"));
                    bean.setCategory1Id(dim.getString("category1_id"));
                }
            },
            60,
            TimeUnit.SECONDS
        );

        //TODO 16.关联category1维度
        SingleOutputStreamOperator<TradeSkuOrderBean> restulStream = AsyncDataStream.unorderedWait(
            c2Stream,
            new DimAsyncFunction<TradeSkuOrderBean>("dim_base_category1") {

                @Override
                public String getKey(TradeSkuOrderBean bean) {
                    return bean.getCategory1Id();
                }

                @Override
                public void join(JSONObject dim,TradeSkuOrderBean bean) {
                    bean.setCategory1Name(dim.getString("name"));
                }
            },
            60,
            TimeUnit.SECONDS
        );
        //TODO 17.将关联的结果写到Doris
        restulStream.print(">>");
        restulStream
            .map(new BeanToJsonStrFunction<>())
            .sinkTo(DorisUtil.getDorisSink("dws_trade_sku_order_window"));
        env.execute();
    }
}
